package com.handinfo.android.ui.window;

import java.io.DataInputStream;
import java.io.IOException;

import android.graphics.Bitmap;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlTools;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWLabel;
import com.handinfo.android.uicontrols.controls.DWMessageBox;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

public class UIFamilyCarbonWindow implements IUIWindows
{

	public Bitmap m_cr_tuichu = null;
	public Bitmap m_cr_shengli = null;
	public Bitmap m_cr_ima = null;

	public DWImageBox m_tit_ima;
	public DWImageBox m_shengli_ima;
	public DWFrame m_count_frame;
	DWTextbox m_lb;
	public DWButton m_djs_ima;
	public DWImageBox m_tc;
	public DWBackground m_xbj_bg;
	public void FamilyCarbonCount()
	{
		if(DWControlsManager.getInstance().contains(m_count_frame))
		{
			if(m_count_frame!= null)
			{
				DWControlsManager.getInstance().removeControl(m_count_frame);
			}
		}
		m_count_frame = new DWFrame(400,m_cr_shengli.getHeight()+m_cr_die_djs.getHeight()+110);
		m_count_frame.setClickClose(false);
		m_count_frame.setThrough(true);
		DWControlsManager.getInstance().addControl(m_count_frame);

		DWTitle m_tit = new DWTitle("", m_count_frame);
		m_tit_ima = new DWImageBox(m_cr_ima);
		m_tit_ima.setNearAnchor(m_tit, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);

		m_tc = new DWImageBox(m_cr_tuichu);
		m_tc.setNearAnchor(m_tit, Tools.RIGHT_VCENTER, Tools.RIGHT_VCENTER, 0, 0);
		m_tc.setTouchZoomIn(40, 40);
		m_tc.addListener(new DWListener() {

			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				DWControlsManager.getInstance().removeControl(m_count_frame);
				m_jiesuan_time = 0;
			}
		});
		
		m_xbj_bg = new DWBackground(m_count_frame.getShowWidth()-20, m_count_frame.getShowHeight()-(m_tit.getShowHeight()-20)-20);
		m_xbj_bg.setNearAnchor(m_count_frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 10, (m_tit.getShowHeight()-20)+10);
		
		m_lb = new DWTextbox(DWControlTools.CHANGE_ROW + "<mark c=FFBB11>" + "妞，给爷笑个个" + "</mark>" + "给予BOSS致命一击。"+
				DWControlTools.CHANGE_ROW +"完成时间：" + "<mark c=00BBFF>" + "20"+ "</mark>" + "分钟" + "<mark c=55FF00>" + "50"
				+ "</mark>" + "秒"  + DWControlTools.CHANGE_ROW +"金钱：" +
				"<mark c=FFBB11>" + "150000000" + "</mark>" +"金",m_xbj_bg.getShowWidth()-m_cr_shengli.getWidth()-20);
		m_lb.setNearAnchor(m_xbj_bg, Tools.LEFT_TOP, Tools.LEFT_TOP, 10, (m_xbj_bg.getShowHeight()-m_lb.getShowHeight()-10-m_cr_die_djs.getHeight())/2);
		
		m_shengli_ima = new DWImageBox(m_cr_shengli);
		m_shengli_ima.setNearAnchor(m_xbj_bg, Tools.RIGHT_VCENTER, Tools.RIGHT_VCENTER, -10,-15);

		m_djs_ima = new DWButton("", m_cr_die_djs);
		m_djs_ima.setNearAnchor(m_lb, Tools.HCENTER_TOP, Tools.HCENTER_BOTTOM, 0, 10);

		m_count_frame.addControl(m_tit);
		m_count_frame.addControl(m_tit_ima);
		m_count_frame.addControl(m_tc);
		m_count_frame.addControl(m_xbj_bg);
		m_count_frame.addControl(m_lb);
		m_count_frame.addControl(m_shengli_ima);
		m_count_frame.addControl(m_djs_ima);
	}

	String m_family_carbon_name;
	String m_key;
	int m_time;
	String m_boss;
	int m_money;
	int m_jiesuan_time;

	///结算
	public void recvFamilyCarbon(DataInputStream dis) throws IOException
	{
		m_family_carbon_name = dis.readUTF();
		m_key = dis.readUTF();
		m_time = dis.readInt();
		m_boss = dis.readUTF();
		m_money = dis.readInt();
		m_jiesuan_time = dis.readInt();
		m_over_count_2 = System.currentTimeMillis();
		FamilyCarbonCount();
		m_lb.setText(DWControlTools.CHANGE_ROW + "<mark c=FFBB11>" + m_boss + "</mark>" + "给予BOSS致命一击。"+ DWControlTools.CHANGE_ROW
				+"完成时间：" + "<mark c=00BBFF>" + m_time / 60 + "</mark>" + "分钟" + "<mark c=55FF00>" + m_time % 60
				+ "</mark>" + "秒" + DWControlTools.CHANGE_ROW +"金钱："+"<mark c=FFBB11>" +m_money  + "</mark>"+ "金");
		
		m_lb.setNearAnchor(m_xbj_bg, Tools.LEFT_TOP, Tools.LEFT_TOP, 10, (m_xbj_bg.getShowHeight()-m_lb.getShowHeight()-10-m_cr_die_djs.getHeight())/2);
		m_djs_ima.setNearAnchor(m_lb, Tools.HCENTER_TOP, Tools.HCENTER_BOTTOM, 0, 10);
	}

	int m_djs;
	int m_die_money;
	///倒计时
	public void recvFamilyDJS(DataInputStream dis) throws IOException
	{
		m_djs = dis.readInt();
		m_die_money = dis.readInt();
		Tools.debugPrintln("m_djs:" + m_djs + "m_die_money:" + m_die_money);
		m_over_count_1 = System.currentTimeMillis();
		if(DWControlsManager.getInstance().contains(UIWindows.getInstance().m_map.m_world_map))
		{
			DWControlsManager.getInstance().removeControl(UIWindows.getInstance().m_map.m_world_map);
		}
		if(DWControlsManager.getInstance().contains(UIWindows.getInstance().m_hook.m_hook_frame))
		{
			DWControlsManager.getInstance().removeControl(UIWindows.getInstance().m_hook.m_hook_frame);
		}
		DJS_Time();
		m_die_djs_tb.setText("您可以使用" + m_die_money + "元宝");
		m_die_djs_tb.setNearAnchor(m_fuhuo_but, Tools.RIGHT_VCENTER, Tools.LEFT_VCENTER, 0, 0);
	}
	
	String m_depr;
	//购买描述
	DWImageBox m_de_but;
	byte m_state;

	public void recvFamilyDepr(DataInputStream dis) throws IOException
	{
		m_state = dis.readByte();
		Tools.debugPrintln("m_state:" + m_state + "m_depr:" + m_depr);
		if (m_state == 0) {
			if (UIWindows.getInstance().m_main.m_Uimain_Frame.contains(m_de_but)) {
				UIWindows.getInstance().m_main.m_Uimain_Frame.removeControl(m_de_but);
				UIWindows.getInstance().m_main.OpenTaskList();
			}
		}
		else if (m_state == 1) {
			m_depr = dis.readUTF();
			UIWindows.getInstance().m_main.closeTaskList();
			if (m_de_but!=null && !UIWindows.getInstance().m_main.m_Uimain_Frame.contains(m_de_but)) {
				UIWindows.getInstance().m_main.m_Uimain_Frame.addControl(m_de_but);
				m_de_but.setSelect(true);
			}
			else if (!UIWindows.getInstance().m_main.m_Uimain_Frame.contains(m_de_but)) {
				m_de_but = new DWImageBox(m_cr_buff);
				m_de_but.setSelect(true);
				m_de_but.setNearAnchor(UIWindows.getInstance().m_main.m_renwuanniu, Tools.RIGHT_VCENTER,
						Tools.LEFT_VCENTER, -5, 0);
				UIWindows.getInstance().m_main.m_Uimain_Frame.addControl(m_de_but);
			}
			m_de_but.addListener(new DWListener() {

				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					final DWMessageBox m_mb = new DWMessageBox("温馨提示", m_depr, "确定", "返回");
					DWControlsManager.getInstance().addControl(m_mb);
					m_mb.addListener(new DWListener() {
						public void OnDoubleClick(){}
						public void OnClick()
						{
							if (m_mb.getResult() == DWMessageBox.SOFTKEY_OK)
							{
								m_de_but.setSelect(false);
								DWGameManager.getInstance().getSendMessage().sendBuyBuff();
							}
							else if (m_mb.getResult() == DWMessageBox.SOFTKEY_BACK)
							{
							}
							DWControlsManager.getInstance().removeControl(m_mb);
						}
					});
				}
			});
		}
	}

	public void init()
	{
		m_cr_tuichu = UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");
		m_cr_shengli = UIWindows.createImage("/img/newui/fubensl_1.gnp");
		m_cr_ima = UIWindows.createImage("/img/newui/fuben_1.gnp");
		m_cr_but = UIWindows.createImage("/img/newui/anniu_6.gnp");
		m_cr_but_anxia = UIWindows.createImage("/img/newui/anniu_6ax.gnp");
		m_cr_die_djs = UIWindows.createImage("/img/newui/fubenshenglidmk_1.gnp");
		m_cr_shibai = UIWindows.createImage("/img/newui/fubensb_1.gnp");
		m_cr_buff = UIWindows.createImage("/img/icon/big_datubiao_242.gnp");
		
	}

	public void open(byte index)
	{
		FamilyCarbonCount();
	}

	public void close(byte index)
	{
	}

	public void dispose()
	{
	}

	public void logic()
	{
		DJS();
		DJS_djs();
	}

	public DWFrame m_djs_frame = null;
	public Bitmap m_cr_but = null;
	public Bitmap m_cr_buff = null;
	public Bitmap m_cr_but_anxia = null;
	public DWButton m_die_djs__but;
	public DWButton m_fuhuo_but;
	public Bitmap m_cr_die_djs = null;
	public DWTextbox m_die_djs_tb;
	public DWBackground m_djs_xbj = null;
	DWLabel m_die_tb ;
	public void DJS_Time()
	{
		if(DWControlsManager.getInstance().contains(m_djs_frame))
		{
			if(m_djs_frame!=null)
			{
				DWControlsManager.getInstance().removeControl(m_djs_frame);
			}
		}
		if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH)
		{
			m_djs_frame = new DWFrame(450, 400);
		}
		else
		{
			m_djs_frame = new DWFrame(350, 300);
		}
		m_djs_frame.setCentre();
		m_djs_frame.setClickClose(false);
		DWControlsManager.getInstance().addControl(m_djs_frame);
		
		m_djs_xbj = new DWBackground(m_djs_frame.getShowWidth()-20, m_djs_frame.getShowHeight()-30);
		m_djs_xbj.setNearAnchor(m_djs_frame, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);
		m_djs_frame.addControl(m_djs_xbj);
		
		m_die_djs__but = new DWButton("", m_cr_die_djs);
		m_die_djs__but.setNearAnchor(m_djs_xbj, Tools.RIGHT_TOP, Tools.RIGHT_TOP, -10, 10);
		m_djs_frame.addControl(m_die_djs__but);

		m_die_tb = new DWLabel("您在战场中已经死亡，请等待复活！");
		m_die_tb.setBackgroundColor(Tools.ALPHA);
		m_die_tb.setShowWideHigh(m_djs_xbj.getShowWidth()-20, m_djs_xbj.getShowHeight()-m_cr_but.getHeight()-10-m_cr_die_djs.getHeight()-10-10);
		m_die_tb.setNearAnchor(m_djs_xbj, Tools.LEFT_TOP, Tools.LEFT_TOP, 10, m_cr_die_djs.getHeight()+10+5);
		m_djs_frame.addControl(m_die_tb);

		m_fuhuo_but = new DWButton("立即复活", m_cr_but);
		m_fuhuo_but.setDownImage(m_cr_but_anxia);
		m_fuhuo_but.setNearAnchor(m_djs_xbj, Tools.RIGHT_BOTTOM, Tools.RIGHT_BOTTOM, -5, -10);
		m_fuhuo_but.addListener(new DWListener() {

			public void OnDoubleClick()
			{
			}
			public void OnClick()
			{
				if (DWGameManager.getInstance().m_role.m_rmb < m_die_money)
				{
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "您的元宝不足，请等待倒计时，结束后复活。");
				}
				else
				{
					DWControlsManager.getInstance().removeControl(m_djs_frame);
					DWGameManager.getInstance().getSendMessage().sendReliveRole();
					m_djs = 0;
				}
			}
		});
		m_djs_frame.addControl(m_fuhuo_but);
		m_die_djs_tb = new DWTextbox("您可以使用元宝复活");
		m_die_djs_tb.setNearAnchor(m_fuhuo_but, Tools.RIGHT_VCENTER, Tools.LEFT_VCENTER, 0, 0);
		m_djs_frame.addControl(m_die_djs_tb);
	}
	public long m_over_count_1 = 0;

	public void DJS()
	{
		if (DWControlsManager.getInstance().contains(m_djs_frame)) {
			if (m_djs > 0) {
				if (m_over_count_1 == 0) {
					m_over_count_1 = System.currentTimeMillis();//读取时截取系统时间
				}
				long currentTime = System.currentTimeMillis();//当前系统时间
				int index = (int) (m_djs - (currentTime - m_over_count_1) / 1000);
				if (index < 0) {
					m_over_count_1 = 0;
				}
				else {
					m_die_djs__but.setName(index + "");
					if (index == 0) {
						DWControlsManager.getInstance().removeControl(m_djs_frame);
						m_djs = 0;
					}
				}
			}
		}
	}

	public long m_over_count_2 = 0;

	public void DJS_djs()
	{
		if (DWControlsManager.getInstance().contains(m_count_frame)) {
			if (m_jiesuan_time > 0) {
				if (m_over_count_2 == 0) {
					m_over_count_2 = System.currentTimeMillis();//读取时截取系统时间
				}
				long currentTime = System.currentTimeMillis();//当前系统时间
				int index = (int) (m_jiesuan_time - (currentTime - m_over_count_2) / 1000);
				if (index < 0) {
					m_over_count_2 = 0;
				}
				else {
					m_djs_ima.setName(index + "");
					if (index == 0) {
						DWControlsManager.getInstance().removeControl(m_count_frame);
						m_jiesuan_time = 0;
					}
				}
			}
		}
	}

	Bitmap m_cr_shibai = null;
	public DWFrame m_shibai_frame = null;
	public void recvFamilyFail(DataInputStream dis) throws IOException
	{
		Tools.debugPrintln("读取副本失败指令。");
		if(DWControlsManager.getInstance().contains(UIWindows.getInstance().m_map.m_world_map))
		{
			DWControlsManager.getInstance().removeControl(UIWindows.getInstance().m_map.m_world_map);
		}
		if(DWControlsManager.getInstance().contains(UIWindows.getInstance().m_hook.m_hook_frame))
		{
			DWControlsManager.getInstance().removeControl(UIWindows.getInstance().m_hook.m_hook_frame);
		}
		
		if(DWControlsManager.getInstance().contains(m_djs_frame))
		{
			if(m_djs_frame != null)
			{
				DWControlsManager.getInstance().removeControl(m_djs_frame);
				m_djs = 0;
			}
		}
		if(DWControlsManager.getInstance().contains(m_shibai_frame))
		{
			if(m_shibai_frame!=null)
			{
				DWControlsManager.getInstance().removeControl(m_shibai_frame);
			}
		}
		m_shibai_frame = new DWFrame(0, 0, DWGameManager.Screen_Width, DWGameManager.Screen_Height);
		m_shibai_frame.m_null_frame = true;
		m_shibai_frame.setShield(true);
		m_shibai_frame.setThrough(true);
		DWImageBox m_shibai_ima = new DWImageBox(m_cr_shibai);
		m_shibai_ima.setNearAnchor(m_shibai_frame, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);
		DWControlsManager.getInstance().addControl(m_shibai_frame);
		m_shibai_frame.addControl(m_shibai_ima);
	}
}
